﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GAE_PI.Models;
using GAE_PI.Exceptions;

namespace GAE_PI.Controllers
{
    public class EditerCatalogueController : Controller
    {
        //
        // GET: /EditerCatalogue/

        public ActionResult EditerCatalogue()
        {
            if (!DBModel.getDBM().isAdmin())
                return RedirectToAction("ConsulterActivites", "ConsulterActivites");
            try
            {
                if (DBModel.getDBM().getActivites().Count() > 0)
                {
                    throw new GAEException("Ce catalogue contient des activités. Vous ne pouvez pas l'éditer");
                }
                Session["hashcatalogue"] = DBModel.getDBM().getHashCatalogue();
            }
            catch(GAEException ex)
            {
                ex.errorModel.ControllerName = "EditerCatalogue";
                ex.errorModel.ControllerAction = "EditerCatalogue";
                return View("Error", ex.errorModel);
            }

            Catalogue model = DBModel.getDBM().getCurrentCatalogue();
            return View(model);
        }

        [HttpPost]
        public ActionResult EditerCatalogue(Catalogue c)
        {
            try
            {
                DBModel.getDBM().modifierCatalogue(c, (byte[])Session["hashcatalogue"]);
            }
            catch (GAEException ex)
            {
                ex.errorModel.ControllerName = "EditerCatalogue";
                ex.errorModel.ControllerAction = "EditerCatalogue";
                return View("Error", ex.errorModel);
            }
            Session["catalogue"] = c.dateDebut.ToShortDateString();
            return RedirectToAction("ConsulterActivites", "ConsulterActivites");
        }

    }
}
